package com.ding.easyme.app.util;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TypeConvert {
	public static byte[] intToByte(int value){
		byte[] buf = new byte[4];
		buf[0] = (byte)(value >> 24);
		buf[1] = (byte)(value >> 16);
		buf[2] = (byte)(value >> 8);
		buf[3] = (byte)value;
		return buf;
	}
	
	public static int intToByte(int value, byte[] buf, int pos){
		try{
			buf[pos] = (byte)(value >> 24);
			buf[pos + 1] = (byte)(value >> 16);
			buf[pos + 2] = (byte)(value >> 8);
			buf[pos + 3] = (byte)value;
			return 4;
		}catch(Exception ex){
			return 0;
		}
	}
	
	public static int byteToInt(byte[] buf){
		try{
			return ((0x000000FF & buf[0]) << 24) | ((0x000000FF & buf[1]) << 16) | ((0x000000FF & buf[2]) << 8) | (0x00FF & buf[3]);
		}catch(Exception ex){
			return 0;
		}
	}
	
	public static int byteToInt(byte[] buf, int pos){
		try{
			return ((0x000000FF & buf[pos]) << 24) | ((0x000000FF & buf[pos + 1]) << 16) | ((0x000000FF & buf[pos + 2]) << 8) | (0x00FF & buf[pos + 3]);
		}catch(Exception ex){
			return 0;
		}
	}
	
	public static byte[] longTo4Byte(long value) {
		byte[] buf = new byte[4];
		buf[0] = (byte)(value >> 24);
		buf[1] = (byte)(value >> 16);
		buf[2] = (byte)(value >> 8);
		buf[3] = (byte)value;
		return buf;
	}
	
	public static byte[] longToByte(long value){
		byte[] buf = new byte[8];
		buf[0] = (byte)(value >> 56);
		buf[1] = (byte)(value >> 48);
		buf[2] = (byte)(value >> 40);
		buf[3] = (byte)(value >> 32);
		buf[4] = (byte)(value >> 24);
		buf[5] = (byte)(value >> 16);
		buf[6] = (byte)(value >> 8);
		buf[7] = (byte)value;		
		return buf;
	}
	
	public static int longToByte(long value, byte[] buf, int pos){
		try{
			buf[pos] = (byte)(value >> 56);
			buf[pos + 1] = (byte)(value >> 48);
			buf[pos + 2] = (byte)(value >> 40);
			buf[pos + 3] = (byte)(value >> 32);
			buf[pos + 4] = (byte)(value >> 24);
			buf[pos + 5] = (byte)(value >> 16);
			buf[pos + 6] = (byte)(value >> 8);
			buf[pos + 7] = (byte)value;
			return 8;
		}catch(Exception ex){
			return 0;
		}		
	}
	
	public static long byteToLong(byte[] buf){
		try{
			return (((long)buf[0] & (long)0xFF) << 56) |
			   (((long)buf[1] & (long)0xFF) << 48) |
			   (((long)buf[2] & (long)0xFF) << 40) |
			   (((long)buf[3] & (long)0xFF) << 32) |
			   (((long)buf[4] & (long)0xFF) << 24) |
			   (((long)buf[5] & (long)0xFF) << 16) |
			   (((long)buf[6] & (long)0xFF) << 8) |
			   ((long)buf[7] & (long)0xFF);		
		}catch(Exception ex){
			return 0;
		}
	}
	
	public static long byteToLong(byte[] buf, int pos){
		try{
			return (((long)buf[pos] & (long)0xFF) << 56) |
				   (((long)buf[pos + 1] & (long)0xFF) << 48) |
				   (((long)buf[pos + 2] & (long)0xFF) << 40) |
				   (((long)buf[pos + 3] & (long)0xFF) << 32) |
				   (((long)buf[pos + 4] & (long)0xFF) << 24) |
				   (((long)buf[pos + 5] & (long)0xFF) << 16) |
				   (((long)buf[pos + 6] & (long)0xFF) << 8) |
				   ((long)buf[pos + 7] & (long)0xFF);
		}catch(Exception ex){
			return 0;
		}
	}
	
	protected static char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
	
	public static String byteToHexStr(byte value){
		return "" + HEX_CHAR[(0xFF & value) >> 4] + HEX_CHAR[0x0F & value];
	}
	
	public static String byteToHexStr(byte[] buffer){
		return byteToHexStr(buffer, 0, buffer.length);
	}
	
	public static String byteToHexStr(byte[] buffer, int startPos, int length){
		String result = "";
		for(int i = 0; i < length; i++){
			result = result + byteToHexStr(buffer[startPos + i]);
		}
		return result;
	}
	
	public static String byteToAscii(byte[] buf){
		String result = "";
		try{
			for(int i = 0; (buf != null) && (i < buf.length) && (buf[i] != 0); i++){
				result = result + (char)buf[i];
			}
		}catch(Exception ex){
			;
		}
		return result;
	}
	
	public static String byteToAscii(byte[] buf, int pos, int len){
		String result = "";
		try{
			for(int i = pos; (buf != null) && (i < buf.length) && (i < (pos + len)) && (buf[i] != 0); i++){
				result = result + (char)buf[i];
			}
		}catch(Exception ex){
			;
		}
		return result;
	}
	
	public static String byteToAscii(byte[] buf, int pos, int len, int eChar){
		String result = "";
		try{
			for(int i = pos; (buf != null) && (i < buf.length) && (i < (pos + len)) && (buf[i] != eChar); i++){
				result = result + (char)buf[i];
			}
		}catch(Exception ex){
			;
		}
		return result;
	}
	
	public static byte[] asciiToByte(String str){
		byte[] buf = null;
		try{
			buf = new byte[str.length()];
			for(int i = 0; i < str.length(); i++){
				buf[i] = (byte)str.charAt(i);
			}
		}catch(Exception ex){
			;
		}
		return buf;
	}
	
	public static int asciiToByte(String str, byte[] buf, int pos){
		int len = 0;
		try{
			for(int i = 0; (buf != null) && (i < str.length()); i++, len++){
				buf[pos + len] = (byte)str.charAt(i);
			}
		}catch(Exception ex){
			;
		}
		return len;
	}
	
	public static String byteToGBK(byte[] buf){
		try{
			return new String(buf, "gbk");
		}catch(Exception ex){
			return "";
		}
	}
	
	
	public static String byteToUCS(byte[] buf){
		String result = "";
		try{
			for(int i = 0; i < buf.length; i += 2){
				result = result + (char)(((0x00FF & buf[i]) << 8) | (0x00FF & buf[i+1]));
			}
		}catch(Exception ex){
			;
		}
		return result;
	}
	
	public static String byteToUCS(byte[] buf, int pos, int len){
		String result = "";
		try{
			for(int i = pos; (i < buf.length) && (i < (pos + len)); i += 2){
				result = result + (char)(((0x00FF & buf[i]) << 8) | (0x00FF & buf[i+1]));
			}
		}catch(Exception ex){
			;
		}
		return result;
	}
	
	public static byte[] ucsToByte(String str){
		byte[] buf = null;
		try{
			buf = new byte[str.length() * 2];
			for(int i = 0; (i < str.length()); i++){
				buf[i * 2] = (byte)(str.charAt(i) >> 8);
				buf[i * 2 + 1] = (byte)(str.charAt(i));
			}
		}catch(Exception ex){
			;
		}		
		return buf;
	}
	
	public static int ucsToByte(String str, byte[] buf, int pos){
		int len = 0;
		try{
			for(int i = 0; i < str.length(); i++, len += 2){
				buf[pos + len] = (byte)(str.charAt(i) >> 8);
				buf[pos + len + 1] = (byte)(str.charAt(i));
			}
		}catch(Exception ex){
			;
		}		
		return len;
	}
	
	public static boolean haveChinease(String str){
		try{
			for(int i = 0; i < str.length(); i++){
				if ((0xFF00 & str.charAt(i)) != 0) {
					return true;
				}
			}
		}catch(Exception ex){
			;
		}
		return false;
	}
	
	public static boolean haveChinease(byte[] buf){
		try{
			for(int i = 0; i < buf.length; i++){
				if (buf[i] < 0){
					return true;
				}
			}
		}catch(Exception ex){
			;
		}
		return false;
	}
	
	public static boolean haveChinease(byte[] buf, int pos, int len){
		try{
			for(int i = pos; (i < buf.length) && (i < (pos + len)); i++){
				if (buf[i] < 0){
					return true;
				}
			}
		}catch(Exception ex){
			;
		}
		return false;
	}

	public static byte[] msgToByte(String str){
		try{
			if (TypeConvert.haveChinease(str)){ //UCS
				return str.getBytes("GBK");
				//return TypeConvert.ucsToByte(str);
			}else{//ASCII
				return TypeConvert.asciiToByte(str);
			}			
		}catch(Exception ex){
			return null;
		}
	}

	public static int msgToByte(String str, byte[] buf, int pos, int len) {
		byte[] b = msgToByte(str);
		if(b.length >= len) {
			System.arraycopy(b, 0, buf, pos, len);
		} else {
			System.arraycopy(b, 0, buf, pos, b.length);
			for(int i=0; i<len-b.length; i++) {
				buf[pos + b.length + i] = 0;
			}
		}
		return len;
 	}
	
	public static int msgToByte(String str, byte[] buf, int pos){
		try{
			if (TypeConvert.haveChinease(str)){ //UCS
				return TypeConvert.ucsToByte(str, buf, pos);
			}else{//ASCII
				return TypeConvert.asciiToByte(str, buf, pos);
			}			
		}catch(Exception ex){
			return 0;
		}
	}
	
	public static String byteToMsg(byte[] buf){
		try{
			if (TypeConvert.haveChinease(buf)){//UCS
				return TypeConvert.byteToUCS(buf);
			}else{//ASCII
				return TypeConvert.byteToAscii(buf);
			}			
		}catch(Exception ex){
			return "";
		}		
	}
	
	public static String byteToMsg(byte[] buf, int pos, int len){
		try{
			if (TypeConvert.haveChinease(buf, pos, len)){//UCS
				byte[] b = new byte[len];
				System.arraycopy(buf, pos, b, 0, len);
				return TypeConvert.byteToGBK(b);
			}else{//ASCII
				return TypeConvert.byteToAscii(buf, pos, len);
			}			
		}catch(Exception ex){
			return "";
		}		
	}
	
	public static String datetimeToStr(String fmtStr, Date dt){
		try{
			return (new SimpleDateFormat(fmtStr)).format(dt);			
		}catch(Exception ex){
			return "";
		}		
	}
	
	//比较两个byte数组的值是否相等，用于对登录用户的登录验证
	public static boolean theSameArr(byte[] buf1, int pos1, byte[] buf2, int pos2, int len){
		try{
			for(int i = 0; i < len; i++){
				if (buf1[pos1 + i] != buf2[pos2 + i]){
					return false;
				}				
			}
			return true;
		}catch(Exception ex){
			return false;
		}
	}
	
	public static long ipToLong(String host){
		try{
			InetAddress na = Inet4Address.getByName(host);
			byte[] buf = na.getAddress();
		    return (((long)(0x00FF & buf[0])) << 24) | (((long)(0x00FF & buf[1])) << 16) | (((long)(0x00FF & buf[2])) << 8) | (0x00FF & buf[3]);
		}catch(Exception ex){
			return 0;
		}
	}
	
	public static String longToIP(long ip){
		try{
			return String.valueOf(0x00FF & (byte)(ip >> 24)) + "." +
			       String.valueOf(0x00FF & (byte)(ip >> 16)) + "." +
			       String.valueOf(0x00FF & (byte)(ip >> 8)) + "." +
			       String.valueOf(0x00FF & (byte)ip);
		}catch(Exception ex){
			return "0.0.0.0";
		}		
	}
	
	public static boolean isByteArrayNull(byte[] buf) {
		for(int i=0; i<buf.length; i++) {
			if(buf[i] != 0) return false;
		}
		return true;
	}
	
	
	public static String toUnicode(String s, String preStr) {
		String as[] = new String[s.length()];
		String s1 = "";
		for (int i = 0; i < s.length(); i++) {
			as[i] = Integer.toHexString(s.charAt(i) & 0xffff);
			s1 = s1 + preStr + as[i];
		}
		return s1;
	}  


}
